package com.github.tonydeng.fmj.runner;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.github.tonydeng.fmj.pool.RedisOpsUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * Created by tonydeng on 15/4/15.
 */
@Slf4j
public class BaseChiaCommandOption {
    private static boolean isWin = false;
    private static boolean isLinux = false;
    private static Integer defaultIoThreads = 1;

    private static List<String> ffmpegBinary;

    public static final String WINCMD = "cmd";
    public static final String WINCMDOP = "/c";
    public static final String LINUXCMD = "/usr/bin/env";
    public static final String WINCHIA = "C:\\Users\\Administrator\\AppData\\Local\\chia-blockchain\\app-1.1.4\\resources\\app.asar.unpacked\\daemon\\chia.exe";
    public static final String LINUXCHIA = "/home/chia-blockchain/venv/bin/chia";

    public static final String K = "-k";
    public static final String N = "-n";
    public static final String R = "-r";
    public static final String U = "-u";
    public static final String B = "-b";
    public static final String F = "-f";
    public static final String P = "-p";
    public static final String T = "-t";
    public static final String T2 = "-2";
    public static final String D = "-d";
    public static final String E = "-e";

    public static final String H264 = "h264";
    public static final String UTF8 = "utf-8";

    static {
        String env = System.getProperty("os.name");
        log.debug("current operate system :{}", env);

        if (StrUtil.isNotEmpty(env)) {
            String os = env.toLowerCase();
            if (os.contains("win")) {
                isWin = true;
            } else if (os.contains("linux") || os.contains("mac")) {
                isLinux = true;
            }
        }
        //获得当前机器的CPU核数
        defaultIoThreads = Runtime.getRuntime().availableProcessors();
        if (log.isDebugEnabled()) {
            log.debug("isWindows : '{}'  or isLinux:'{}' defaultIoThreads:'{}'",
                    isWin, isLinux, defaultIoThreads);
        }
    }

    private BaseChiaCommandOption() {

    }

    /**
     * 得到p盘命令参数
     *
     * @return
     */
    public static List<String> getChiaPlotsBinary() {
        if (ffmpegBinary == null) {
            if (isWin) {
                ffmpegBinary = CollUtil.newArrayList(WINCMD, WINCMDOP, WINCHIA);
            } else if (isLinux) {
                ffmpegBinary = CollUtil.newArrayList(LINUXCMD, LINUXCHIA);
            }
        }
        return ffmpegBinary;
    }

    /**
     * p盘的命令参数
     *
     * @return
     */
    public static List<String> toPlotsCmdArrays(RedisOpsUtil redisOpsUtil, String workId, String size, String num, String bytes,
                                                String theadNum, String buckets,
                                                String farmerKey, String poolKey,
                                                String tmpDir, String tmp2Dir, String finalDir) {
        List<String> commands = CollUtil.newArrayList();
        commands.add("plots");
        commands.add("create");
        commands.add(K);
        commands.add(size);
        commands.add(N);
        commands.add(num);
        commands.add(R);
        commands.add(theadNum);
        commands.add(B);
        commands.add(bytes);
        commands.add(E);

        if (StrUtil.isNotEmpty(buckets)) {
            commands.add(U);
            commands.add(buckets);
        }

        commands.add(F);
        commands.add(farmerKey);
        commands.add(P);
        commands.add(poolKey);
        commands.add(T);
        commands.add(tmpDir + "/" + workId);

        redisOpsUtil.setValue("tempDir_"+ workId, tmpDir + "/" + workId);

//        if (StrUtil.isNotEmpty(tmp2Dir)) {
//            commands.add(T2);
//            commands.add(tmp2Dir + "/" + workId);
//
//            redisOpsUtil.setValue("tempDir2_"+ workId, tmp2Dir + "/" + workId);
//        }

        commands.add(D);
        commands.add(finalDir);
        return commands;
    }
}
